<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\common\utils;

/**
 * Description of DeleteFile
 *
 * @author yuilin
 */
class DeleteFile {

//循环目录下的所有文件
    public static function delFileUnderDir($dirName = "./browseTemp") {
        if ($handle = opendir("$dirName")) {
            while (false !== ( $item = readdir($handle) )) {
                if ($item != "." && $item != "..") {
                    if (is_dir("$dirName/$item")) {
                        if (DeleteFile::is_empty_dir("$dirName/$item")) {//如果是空的
                            rmdir("$dirName/$item"); //直接删除
                        } else {
                            DeleteFile::delFileUnderDir("$dirName/$item");
                        }
                    } else {
                        if (unlink("$dirName/$item"))
                            echo "成功删除文件： $dirName/$item<br />n";
                    }
                }
            }
            closedir($handle);
        }
    }

    public static function delDirAndFile($dirName) {
        if ($handle = opendir("$dirName")) {
            while (false !== ( $item = readdir($handle) )) {
                if ($item != "." && $item != "..") {
                    if (is_dir("$dirName/$item")) {
                        DeleteFile::delDirAndFile("$dirName/$item");
                    } else {
                        if (unlink("$dirName/$item"))
                            echo "成功删除文件： $dirName/$item<br />n";
                    }
                }
            }
            closedir($handle);
            if (rmdir($dirName))
                echo "成功删除目录： $dirName<br />n";
        }
    }

    public static function is_empty_dir($pathdir) {
//判断目录是否为空
        $d = opendir($pathdir);
        $i = 0;
        while ($a = readdir($d)) {
            $i++;
        }
        closedir($d);
        if ($i > 2) {
            return false;
        } else
            return true;
    }

}

?>